var love 
export default class touch{
  constructor(){
    this.touches = {}
    //todo 滑动 圈选 双滑动 分离 合并 旋转
  }
  load(love){
    this.love = love
    this.boundStart = this.ouTouchStart.bind(this)
    this.boundMove = this.onTouchMove.bind(this)
    this.boundEnd = this.onTouchEnd.bind(this)
    this.boundCancel = this.onTouchCancel.bind(this)
    this.scale = {x :1,y:1}
    var info = wx.getSystemInfoSync()
    //console.log(info)
    if (love.graphics.w < love.graphics.h) {
      this.scale.x = love.graphics.designResolution.w / love.graphics.h
      this.scale.y = love.graphics.designResolution.h / love.graphics.w
    } else {
      this.scale.x = 1 / love.graphics.adjustScale.x
      this.scale.y = 1 / love.graphics.adjustScale.y
    }
    wx.onTouchStart(this.boundStart)
    wx.onTouchMove(this.boundMove)
    wx.onTouchEnd(this.boundEnd)
    wx.onTouchCancel(this.boundCancel)
  }
  enableDebug(t){
    this.debug = t
  }
  unload(){
    wx.offTouchStart(this.boundStart)
    wx.offTouchMove(this.boundMove)
    wx.offTouchEnd(this.boundEnd)
    wx.offTouchCancel(this.boundCancel)
  }
  ouTouchStart(res){
    for (var i in res.touches) {
      var touch = res.touches[i]
      
      var ox = touch.pageX * this.scale.x
      var oy = touch.pageY * this.scale.y
      this.love.ui.onTouch(ox,oy)
      this.touches[touch.identifier] = 
        { x :ox,y:oy ,ox:ox,oy:oy,px : touch.pageX,py:touch.pageY,
        dx: 0, dy: 0, id : touch.identifier}
      if (!this.love.ui.active){
        this.onTouch.bind(this.love)(this.touches[touch.identifier])
      }
    }
    
  }
  onTouchMove(res){
    for (var i in res.touches) {
      var touch = res.touches[i]
      var target = this.touches[touch.identifier]
      if (target) {
        target.dx = touch.pageX * this.scale.x - target.x
        target.dy = touch.pageY * this.scale.y - target.y
        target.x = touch.pageX * this.scale.x
        target.y = touch.pageY * this.scale.y
        if (!this.love.ui.active) {
          this.onTouch.bind(this.love)(this.touches[touch.identifier])
        }
      } else {
        var touch = res.touches[i]
        this.touches[touch.identifier] =
          {
            x: touch.pageX * this.scale.x, y: touch.pageY*this.scale.y,
          dx: 0, dy: 0, id : touch.identifier
          }
        if (!this.love.ui.active) {
          this.onTouch.bind(this.love)(this.touches[touch.identifier])
        }
      }
    }
    
  }
  update(dt){
    for (var id in this.touches) {
      let touch = this.touches[id]
      this.love.ui.onHold(touch.x, touch.y,dt)
      if (!this.love.ui.active) {
        this.onTouch.bind(this.love)(touch)
      }
    }
  }
  onTouchCancel(res){
    this.touches = []
  }
  onTouchEnd(res){
    for (var id in this.touches) {
      let touch = this.touches[id]
      let dist = Math.abs(touch.ox - touch.x) + Math.abs(touch.oy - touch.y)
      if(dist>100){
        this.onSwipe.bind(this.love)(touch)
      }
    }
    this.touches = []
  }
  draw(){
    if (this.debug) {
      this.love.graphics.setColor("red")
      for (var id in this.touches) {
        let touch = this.touches[id]
        this.love.graphics.circle("line",touch.x,touch.y,40)
        this.love.graphics.circle("fill", touch.ox, touch.oy, 40)
        this.love.graphics.print(touch.px+","+touch.py,touch.x+100,touch.y)
      } 
    }
  }
  onTouch(){}
  onSwipe(){}
}